package service

import (
	"example.com/m/v2/model/mysql"
	"example.com/m/v2/types"
	"gorm.io/gorm"
)

type Imgs struct {
	BaseService
	db *gorm.DB
}

func NewImgs(svcCtx *ServiceContext, db *gorm.DB) *Imgs {
	res := Imgs{db: db, BaseService: BaseService{svcCtx: svcCtx}}
	return &res
}

func (imgs *Imgs) GetListByPage(req *types.DesignImgsReq) (*[]mysql.Imgs, int64) {
	var res []mysql.Imgs
	var total int64
	query := imgs.db.Model(&res)

	if req.Cate != 0 {
		query.Where("category=?", req.Cate)
	}
	page := 1
	if req.Page != 0 {
		page = req.Page
	}

	pageSize := 20
	if req.PageSize != 0 {
		pageSize = req.PageSize
	}

	query.Count(&total)
	query.Offset(pageSize * (page - 1)).Limit(pageSize).Find(&res)
	return &res, total
}
